/**
 *查询设备
 */
'use strict';

const conn = require('../../libs/DB/Conn');
const _ = require("underscore");

module.exports = async function (params) {
    let querySql =`SELECT
                        df.device_id AS deviceId,
                        df.device_no AS deviceNo,
                        df.device_type AS deviceType,
                        df.device_name AS deviceName,
                        rf.range_name AS rangeName,
                        df.range_id AS rangeId,
                        df.parent_id AS parentId,
                        cdf.device_id AS cDeviceId,
                        cdf.device_no AS cDeviceNo,
                        cdf.device_type AS cDeviceType,
                        cdf.device_name AS cDeviceName,
                        cdf.parent_id AS cParentId,
                        cdf.range_id AS cRangeId,
                        crf.range_name AS cRangeName
                    FROM
                        device_file df 
                        INNER JOIN range_file rf ON rf.range_id = df.range_id AND rf.delete_sign = 1
                        LEFT JOIN device_file cdf ON cdf.parent_id = df.device_id AND cdf.delete_sign = 1 AND cdf.enable = 1
                        LEFT JOIN range_file crf ON crf.range_id = cdf.range_id AND crf.delete_sign = 1 
                    WHERE
                    df.enable = 1 AND df.parent_id = 0 AND 
                    df.delete_sign = 1 AND df.range_id IN (?) 
                    ${params.deviceType ? `AND df.device_type = ${params.deviceType}`:``}
                    ${params.searchText ? `AND (df.device_name LIKE '%${params.searchText}%' OR df.device_no LIKE '%${params.searchText}%'` : ""}`
    let deviceList =  await conn(querySql,[params.rangeIds]);
    let cParentGroup = _.groupBy(deviceList,"cParentId");
    let dataList =[];
    for(const cParentId in cParentGroup){
        let cParentList = cParentGroup[cParentId];
        if(cParentId === "null"){
            for(const parentDevice of cParentList){
                dataList.push({
                    deviceId:parentDevice.deviceId,
                    deviceNo:parentDevice.deviceNo,
                    deviceType:parentDevice.deviceType,
                    deviceName:parentDevice.deviceName,
                    rangeName:parentDevice.rangeName,
                    rangeId:parentDevice.rangeId,
                    parentId:parentDevice.parentId,
                })
            }
        }else{
            let parentObj = {
                deviceId:cParentList[0].deviceId,
                deviceNo:cParentList[0].deviceNo,
                deviceType:cParentList[0].deviceType,
                deviceName:cParentList[0].deviceName,
                rangeName:cParentList[0].rangeName,
                rangeId:cParentList[0].rangeId,
                parentId:cParentList[0].parentId,
            }
            for(const childrenDevice of cParentList){
                dataList.push({
                    deviceId:childrenDevice.cDeviceId,
                    deviceNo:childrenDevice.cDeviceNo,
                    deviceType:childrenDevice.cDeviceType,
                    deviceName:childrenDevice.cDeviceName,
                    rangeName:childrenDevice.cRangeName,
                    rangeId:childrenDevice.cRangeId,
                    parentId:childrenDevice.cParentId,
                })

            }
            dataList.push(parentObj);
        }
    }
    return { success: true ,deviceInfoList:dataList};
};